import { hasClass } from '@/global/utils/index'

export default {
    inserted(el, binding, vnode) {
        const { value } = binding
        if (value === undefined) {
            return
        }

        let btnType = null
        if (el.parentElement.tagName === 'FOOTER' || el.parentElement.parentElement.tagName === 'FOOTER') {
            btnType = 'FOOTER_BTN$'
        } else if (hasClass(el.parentElement, 'el-table-toolbar-right') || hasClass(el.parentElement.parentElement, 'el-table-toolbar-right')) {
            btnType = 'TOOLBAR_BTN$'
        } else if (hasClass(el, 'el-link') && el.parentElement.parentElement.tagName('TD')) {
            btnType = 'COLUMN_BTN$'
        } else {
            btnType = 'OTHER$'
        }
        const pcode = btnType + value
        vnode.context.$store.dispatch('user/hasPermission', { 'href': vnode.context.$route.meta.href, 'pcode': pcode }).then(rst => {
            if (!rst) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        })
    }
}
